var util = require('../../utils/util.js');
var api = require('../../config/api.js');

Page({
  data: {
    navList: [],
    goodsList: [],
    id: 1,
    currentCategory: {},
    scrollLeft: 0,
    scrollTop: 0,
    scrollHeight: 0,
    page: 1,
    limit: 10
  },
  onLoad: function(options) {
    // 页面初始化 options为页面跳转所带来的参数
    var that = this;
    if (options.id) {
      that.setData({
        id: parseInt(options.id)
      });
    }

    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          scrollHeight: res.windowHeight
        });
      }
    });


    this.getCategoryInfo();
  },
  getCategoryInfo: function() {
    let that = this;
    util.request(api.GoodsCategory).then(function(res) {
        if (res.returnValue == 1) {
          that.setData({
            navList: res.data,
            // currentCategory: res.proList
          });

          // wx.setNavigationBarTitle({
          //   title: res.data.parentCategory.name
          // })

          // 当id是L1分类id时，这里需要重新设置成L1分类的一个子分类的id
          // if (res.data.parentCategory.id == that.data.id) {
          //   that.setData({
          //     id: res.data.currentCategory.id
          //   });
          // }

          //nav位置
          let currentIndex = 0;
          let navListCount = that.data.navList.length;
          for (let i = 0; i < navListCount; i++) {
            currentIndex += 1;
            if (that.data.navList[i].id == that.data.id) {
              break;
            }
          }
          if (currentIndex > navListCount / 2 && navListCount > 5) {
            that.setData({
              scrollLeft: currentIndex * 60
            });
          }
          that.getGoodsList();

        } else {
          //显示错误信息
        }

      });
  },
  //获取类别商品api.GoodsList
  getGoodsList: function() {
    var that = this;
    console.log("访问链接：："+api.GoodsList)
    console.log("参数id：："+that.data.id)
    util.request(api.GoodsList, {typeId: that.data.id}).then(function(res) {
        console.log("商品数量：："+res.data.length)
        that.setData({
          currentCategory: res.data
        });
      });
  },
  onReady: function() {
    // 页面渲染完成
  },
  onShow: function() {
    // 页面显示
  },
  onHide: function() {
    // 页面隐藏
  },
  
  onUnload: function() {
    // 页面关闭
  },
  //点击类别 获取商品 event：点击板块的属性参数
  switchCate: function(event) {
    console.log("did::"+this.data.id)
    console.log("event.currentTarget.dataset.id:::"+event.currentTarget.dataset.id)
    if (this.data.id == event.currentTarget.dataset.id) {
      return false;
    }
    var that = this;
    //当前属性X坐标
    var clientX = event.detail.x;
    var currentTarget = event.currentTarget;  
    console.log("clientX:::"+clientX)
    console.log("clientX:::"+this.data.id)
    if (clientX < 60) {
      that.setData({
        scrollLeft: currentTarget.offsetLeft - 60
      });
    } else if (clientX > 330) {
      that.setData({
        scrollLeft: currentTarget.offsetLeft
      });
    }
    this.setData({
      id: event.currentTarget.dataset.id
    });

    this.getCategoryInfo();
  }
})